package controller.editor;

import java.util.List;

import main.model.Diagram;
import main.model.Entity;
import main.model.Relation;
import view.editor.AddEntityToRelationView;
import controller.Controller;

public class AddEntityToRelationController extends Controller{
	
	private Relation relation;
	private Diagram diagram;
	private AddEntityToRelationView view;
	private RelationCreationController controller;
	
	public AddEntityToRelationController(Diagram diagram, Relation r, RelationCreationController controller){
		this.diagram = diagram;
		this.relation = r;
		this.controller = controller;
	}

	@Override
	public void displayView() {
		String[] entities = new String[diagram.getEntitiesNames().size()];
		Integer i = 0;
		for (String string : diagram.getEntitiesNames()) {
			entities[i] = string;
			i++;
		}
		view = new AddEntityToRelationView(entities);
		view.setController(this);
		view.show();
	}

	public void handleButtonTerminar(){
		List<String> selectedEntities = view.getEntitiesList();
		for (Entity entity : diagram.getEntities()) {
			for (String string : selectedEntities) {
				if (entity.getName().equals(string)){
					relation.addEntity(entity);
				}
			}
		}
		controller.addEntityToRelation(selectedEntities);
		view.hide();
	}

}
